﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    public class NPCUIBuildingPage : NPCUIPageTemplete
    {
        string id;
        float cdTime;
        float cdTimeTemp;
        
        public float Timer
        {
            set
            {
                this.cdTime = value;
                if (cdTime <= 1f)
                {
                    cdTimer.gameObject.SetActive(false);
                }
                this.cdTimer.text = string.Format(TableTextBlo.GetText("operation_cdTime_tips"),(int)cdTime);
            }
            get
            {
                return cdTime;
            }
        }

        public UILabel cdTimer;
        List<UISprite> masks = new List<UISprite>();
        public override void SendCSMsg(object o)
        {
            base.SendCSMsg(o);

            page.SetActive(true);
            GetComponent<PageTweener>().Play();
            Fresh();
        }
        
        public void Fresh()
        {
            UIUnionFightRunMap.GetNetData().SendCSUFStrengthBuildingInfo("");
        }

        public void UpdateList(List<UFStrengthBuildingInfo> list,float cdTime)
        {
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();
            masks.Clear();
            pref.SetActive(false);
            for (int i = 0; i < list.Count; i++)
            {
                GameObject building = Instantiate(pref);
                building.SetActive(true);
                var script = building.GetComponent<NPCUIBuildingItem>();
                script.SetItem(list[i],this);
                masks.Add(script.cdMask);
                simpleGrid.AddChild(building);
            }
            this.Timer = cdTime;
            cdTimeTemp = cdTime;
        }

        void Update()
        {
            if(Timer > 0)
            {
                cdTimer.gameObject.SetActive(true);
                Timer -= Time.deltaTime;
                SetCdMask(Timer);
            }
        }

        void SetCdMask(float time)
        {
            for(int i= 0; i < masks.Count; i++)
            {
                masks[i].fillAmount = time / cdTimeTemp;
            }
        }
    }
}
